<template>
  <div class="login">
    <div class="box">
      <h2>商标注册系统</h2>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item label prop="id">
          <el-input placeholder="请输入账号" v-model="ruleForm.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label prop="pass">
          <el-input
            type="password"
            placeholder="请输入密码"
            show-password
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item class="verify" label prop="verify">
          <el-input placeholder="请输入验证码" v-model="ruleForm.verify" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <p class="text">
            <el-checkbox v-model="checked">记住信息</el-checkbox>
            <router-link to="/enrolls" class="register-link">注册账号?</router-link>
            <router-link to="/wp" class="register-link">测试</router-link>
          </p>
        </el-form-item>
        <el-form-item>
          <div class="login-btn">
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          </div>
        </el-form-item>
      </el-form>
      <div class="forget">
        <div class="line"></div>
        <el-button class="pass" type="text">忘记密码</el-button>
        <div class="line"></div>
      </div>
      <div class="home-link">
        <el-button type="primary" icon="el-icon-s-home"></el-button>
      </div>
    </div>
    <el-dialog title="提示" :visible.sync="dialogVisible"  width="30%">
        <p>
          在办理业务前请阅读
          <i>以下须知</i>
        </p>
        <p>
          请仔细核对申请人信息如有遗漏请 点击
          <i>修正</i> 查看详情
        </p>
        <p>
          请仔细核对申请人信息如有错误 点击
          <i>修正</i> 查看详情
        </p>
        <p>
          <i>申请人信息错误将影响申请进度</i>
        </p>
        <p>
          <span>——成都商标受理窗口</span>
        </p>
    </el-dialog>
  </div>
</template>
<script>
import instance from '../../assets/js/axios'

export default {
  name: "login",
  data() {
    return {
      dialogVisible: true,

      checked: false,
      ruleForm: {
        pass: "",
        id: "",
        verify: ""
      },
      rules: {
        id: [{ required: true, message: "请输入账号名", trigger: "blur" }],
        pass: [{ required: true, message: "请输入密码", trigger: "blur" }],
        verify: [{ required: true, message: "请输入验证码", trigger: "blur" }]
      }
    };
  },

  watch: {
    checked() {
      if (this.checked) {
        localStorage.id = this.ruleForm.id;
        localStorage.pass = this.ruleForm.pass;
        localStorage.checked = this.checked;
      } else {
        localStorage.removeItem("id");
        localStorage.removeItem("pass");
        localStorage.removeItem("checked");
      }
    }
  },

  methods: {
    open2() {
      let _this = this;
      this.$message({
        message: "登录成功",
        type: "success",
        duration: 1000,
        onClose(){
          _this.$router.push({
            path: "/home",
          });
        }
      });
    },
    open3() {
      this.$message({
        message: "账号密码错误",
        type: "error",
        duration: 2000
      });
    },
    submitForm(formName) {
      let _this = this;
      this.$refs[formName].validate(valid => {
        if (valid) {
          instance({
            url: "./jia.json",
            method: "get"
          }).then(result => {
            let { user, pass } = result.data;
            if (user == this.ruleForm.id && pass == this.ruleForm.pass) {
              window.localStorage.setItem("token", "1"); //将token登录标志存入本地
              console.log(result);
              this.open2()
            } else {
              this.open3();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  },
    //该路由守卫用于从登录页面跳转出去时恢复状态
  beforeRouteLeave(to,from,next){
      from.meta.judegLogin =true
      next()
  },
  beforeMount() {
    this.dialogVisible = this.$route.meta.judegLogin

    this.ruleForm.id = localStorage.id;
    this.ruleForm.pass = localStorage.pass;
    this.checked = Boolean(localStorage.checked);
  }
};
</script>
<style scoped lang="less">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  background-image: url("../../assets/img/bg.jpg");
  background-size: 100% 100%;
}
.box {
  padding: 20px 40px;
  width: 400px;
  min-height: 400px;
  background-color: #ffffff50;
  border-radius: 5px;
  transition: 0.5s;

  h2 {
    margin: 0 0 30px;
    width: 100%;
    height: 30px;
    color: #606266;
    font-weight: 400;
    text-align: center;
  }
  .verify {
    width: 50%;
  }
  .login-btn {
    width: 100%;
    background: #409eff;
    .el-button {
      width: 100%;
    }
  }
  .suer {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    height: 160px;
    text-align: left;
    .verify {
      width: 50%;
    }
  }
  .text {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 50px;
    .el-button--text {
      color: #606266;
    }
  }
  .home-link {
    display: flex;
    justify-content: center;
  }
  .register-link {
    color: #606266;
    &:hover {
      text-decoration: none;
    }
  }
  .forget {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 50px;
    .el-button--text {
      margin: 0 10px;
      color: #606266;
    }
    .line {
      width: 50%;
      height: 1px;
      background-color: #fff;
    }
  }

  &:hover {
    background-color: #ffffff;
    h2 {
      color: #999;
    }
    .line {
      background-color: #999999;
    }
  }
}
</style>